Skip to content

ref(grouping): Simplify handling of variant name and exception data #97458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 8, 2025

Conversation

lobsterkatie
Copy link
Member

During grouping, we pass data from one component strategy to another by storing it in the grouping context. In that context, two values - the variant name and a blob of exception data - are only sometimes defined. Until recently, the GroupingContext class supported key-lookup using subscript notation (context[some_key]) the same way a dictionary would, but lacked the safer .get() method. We therefore currently initialize both values to None, to prevent raising a KeyError if the values haven't been set.

Now that .get() has been implemented, though, we don't need to do that. This PR therefore switches the way we access them to use .get(), and removes the default initialization. In order to enforce when variant_name is or isn't expected to be defined, it also extends the practice already followed in some strategies of asserting on its existence (or lack thereof). This ensures that if we ever make changes to the grouping code, we'll know right away if our assumptions have been violated.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Aug 8, 2025
@lobsterkatie lobsterkatie marked this pull request as ready for review August 8, 2025 14:44
@lobsterkatie lobsterkatie requested a review from a team as a code owner August 8, 2025 14:44
Copy link
Member

@yuvmen yuvmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good 🎉 , minor comment

@@ -27,9 +27,6 @@
],
delegates=["frame:v1", "stacktrace:v1", "single-exception:v1"],
initial_context={
# This key in the context tells the system which variant should
# be produced. TODO: phase this out.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah the joy of removing a 4 year old TODO in code :)

@@ -72,15 +72,17 @@ def normalize_message_for_grouping(message: str, event: Event) -> str:
def message_v1(
interface: Message, event: Event, context: GroupingContext, **kwargs: Any
) -> ReturnedVariants:
variant_name = context["variant_name"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the code previously didnt, but should we not asserting for existence here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The produces_variants decorator (indirectly) sets variant_name in the context, so we know it will be there.

(To be more precise, the decorator calls call_with_variants, and call_with_variants sets variant_name immediately before calling strategy_func, which in this case is the function message_v1 that you're looking at.)

@lobsterkatie lobsterkatie merged commit f0949c7 into master Aug 8, 2025
64 checks passed
@lobsterkatie lobsterkatie deleted the kmclb-use-get-for-variant-and-exception-data branch August 8, 2025 22:24
andrewshie-sentry pushed a commit that referenced this pull request Aug 12, 2025
…97458)

During grouping, we pass data from one component strategy to another by storing it in the grouping context. In that context, two values - the variant name and a blob of exception data - are only sometimes defined. Until recently, the `GroupingContext` class supported key-lookup using subscript notation (`context[some_key]`) the same way a dictionary would, but lacked the safer `.get()` method. We therefore currently initialize both values to `None`, to prevent raising a `KeyError` if the values haven't been set.

Now that `.get()` has been implemented, though, we don't need to do that. This PR therefore switches the way we access them to use `.get()`, and removes the default initialization. In order to enforce when `variant_name` is or isn't expected to be defined, it also extends the practice already followed in some strategies of asserting on its existence (or lack thereof). This ensures that if we ever make changes to the grouping code, we'll know right away if our assumptions have been violated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants